package com.pro.control;

import com.pro.entity.*;
import com.pro.service.CourseService;
import com.pro.utils.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/*
    赵自强
    2021/11/4
    课程serviceImple
*/
@RestController
@RequestMapping("/course")
public class CourseControl {

    @Autowired
    private CourseService courseService;

    //课程分类 赵自强 2021/11/4
    @RequestMapping("/getCourseClass")
    public JsonData getCourseClass(){

        List<CourseClass> courseClass = courseService.getCourseClass();
        List<CourseClass> newCourseClass = new ArrayList<>();
        for (int i = 0; i<courseClass.size();i++ ){
            if(courseClass.get(i).getParentId() == 0){
                newCourseClass.add(courseClass.get(i));
            }
            for (int k = 0; k<courseClass.size();k++ ){
                if(courseClass.get(i).getId() == courseClass.get(k).getParentId()){
                    courseClass.get(i).getChildren().add(courseClass.get(k));
                }
            }
        }
        JsonData jsonData = new JsonData();

        return newCourseClass!=null?jsonData.buildSuccess(newCourseClass):jsonData.buildError("服务器错误");
    }

    //根据课程发呢类查询数据  赵自强  2021/11/4
    @RequestMapping("/getCourse")
    public JsonData getJavaImg(Integer classId) {
        List<Course> course = courseService.getCourse(classId);
        JsonData jsonData = new JsonData();
        return course!=null?jsonData.buildSuccess(course):jsonData.buildError("服务器错误");

    }

    //课程列表高级检索 赵自强 21/11/9
    @RequestMapping("/advancedRetrieval")
    public JsonData advancedRetrieval(Integer classId, String courseName, Double coursePrice, String trueName, Integer courseCstatus, String courseLabel){
        List<Course> courses = courseService.advancedRetrieval(classId, courseName, coursePrice, trueName, courseCstatus, courseLabel);
        JsonData jsonData = new JsonData();
        return courses!=null?jsonData.buildSuccess(courses):jsonData.buildError("服务器错误");
    }

    //课程列表普通检索 赵自强 21/11/9
    @RequestMapping("/ordinarySearch")
    public JsonData ordinarySearch(String name,String courseName,String trueName,String courseLabel){
        List<Course> courses = courseService.ordinarySearch(name,courseName,trueName,courseLabel);
        JsonData jsonData = new JsonData();
        return courses!=null?jsonData.buildSuccess(courses):jsonData.buildError("服务器错误");
    }

    //根据id查询课程 赵自强 21/11/9
    @RequestMapping("/getCourseById")
    public JsonData getCourseById(Integer courseId){
        Course courseById = courseService.getCourseById(courseId);
        JsonData jsonData = new JsonData();
        return courseById!=null?jsonData.buildSuccess(courseById):jsonData.buildError("服务器错误");
    }

    //根据id 修改课程信息 赵自强 21/11/9
    @RequestMapping("/courseEdit")
    public JsonData courseEdit(Course course){
        Boolean aBoolean = courseService.courseEdit(course);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }


    //课程日志 21//11/10
    @RequestMapping("/getCourseJournals")
    public JsonData getCourseJournals(){
        List<Course> courseJournals = courseService.getCourseJournals();
        JsonData jsonData = new JsonData();
        return courseJournals!=null?jsonData.buildSuccess(courseJournals):jsonData.buildError("服务器错误");
    }

    //课程日志修改价格后添加至日志 记录课程历史价格 赵自强 21/11/10
    @RequestMapping("/addCourseJournal")
    public JsonData addCourseJournal(Course course){
        Boolean aBoolean = courseService.addCourseJournal(course);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }

    //课程列表审核详情信表格数据 赵自强 21/11/11
    @RequestMapping("/getAuditDetalis")
    public JsonData getAuditDetalis(Integer courseId,Integer type){
        List<CourseRecord> auditDetalis = courseService.getAuditDetalis(courseId,type);
        JsonData jsonData = new JsonData();
        return auditDetalis!=null?jsonData.buildSuccess(auditDetalis):jsonData.buildError("服务器错误");
    }

    //根据id删除课程列表其中一条数据 赵自强 21/11/11
    @RequestMapping("/delCourse")
    public JsonData delCourse(Integer courseId){
        Boolean aBoolean = courseService.delCourse(courseId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }

    //添加课程 赵自强 21/11/11
    @RequestMapping("/addCourse")
    public JsonData addCourse(Course course){
        Boolean aBoolean = courseService.addCourse(course);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }


    //课程操做日志查询 赵自强 21/11/14
    @RequestMapping("/operationLog")
    public JsonData operationLog(){
        List<Course> courses = courseService.operationLog();
        JsonData jsonData = new JsonData();
        return courses!=null?jsonData.buildSuccess(courses):jsonData.buildError("服务器错误");
    }

    //课程操作日志添加 赵自强 21/11/14
    @RequestMapping("/addoperationLog")
    public JsonData addoperationLog(Course course){
        Boolean aBoolean = courseService.addoperationLog(course);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }

    //课程列表ztree树 添加树节点 赵自强 21/11/15
    @RequestMapping("/addZtreeNode")
    public JsonData addZtreeNode(CourseClass courseClass){
        Boolean aBoolean = courseService.addZtreeNode(courseClass);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }

    //课程列表ztree树 修改树节点 赵自强 21/11/15
    @RequestMapping("/editZtreeNode")
    public JsonData editZtreeNode(CourseClass courseClass){
        Boolean aBoolean = courseService.editZtreeNode(courseClass);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }

    //课程列表ztree树 删除树节点 赵自强 21/11/15
    @RequestMapping("/delZtreeNode")
    public JsonData delZtreeNode(CourseClass courseClass){
        Boolean aBoolean = courseService.delZtreeNode(courseClass);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }



    //操作日志检索
    @RequestMapping("/operationLogRetrieval")
    public JsonData operationLogRetrieval(Integer dictSort,String trueName){
        List<Course> courses = courseService.operationLogRetrieval(dictSort, trueName);
        JsonData jsonData = new JsonData();
        return courses!=null?jsonData.buildSuccess(courses):jsonData.buildError("服务器错误");
    }

    //历史详情价格检索
    @RequestMapping("/journalRetrieval")
    public JsonData journalRetrieval(String courseName,String name,String trueName,Integer courseCstatus){
        List<Course> courses = courseService.journalRetrieval(courseName, name, trueName, courseCstatus);
        JsonData jsonData = new JsonData();
        return courses!=null?jsonData.buildSuccess(courses):jsonData.buildError("服务器错误");
    }


    //导入excel表格 赵自强 21/12/9
    @RequestMapping(value = "/importExcel")
    @ResponseBody
    public JsonData importExcel(MultipartFile file){
        Course course = new Course();

        String originalFilename = file.getOriginalFilename();

        InputStream in = null;
        try {
            //将file转InputStream
            in = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*if (originalFilename.substring(originalFilename.indexOf(".")) == "xlsx"){
            //将InputStream转XLS对象（即表格对象）
            XSSFWorkbook xssfWorkbook = null;
        }
        if (originalFilename.substring(originalFilename.indexOf("." )) == "xls"){
            HSSFWorkbook hssfWorkbook = null;
        }*/
        XSSFWorkbook xssfWorkbook = null;
        try {
            xssfWorkbook = new XSSFWorkbook(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //获取表格的第一页
        Sheet xssfSheet = xssfWorkbook.getSheetAt(0);
        //获取该页有多少数据
        int rowNum= xssfSheet.getLastRowNum();
        System.err.println(rowNum);
        for(int i=1; i<=rowNum; i++){
            //获取当前行
            Row xssfRow = xssfSheet.getRow(i);
            //以此获得该行的所有单元格
            String userId = xssfRow.getCell(1).toString();
            String classId = xssfRow.getCell(2).toString();
            String  recommend = xssfRow.getCell(3).toString();
            String courseName = xssfRow.getCell(4).toString();
            String courseIntro = xssfRow.getCell(5).toString();
            String coursePrice = xssfRow.getCell(6).toString();
            String  courseNprice = xssfRow.getCell(7).toString();
            String courseStatus = xssfRow.getCell(8).toString();
            String courseCstatus = xssfRow.getCell(9).toString();
            String coursePrimary = xssfRow.getCell(10).toString();
            String url = xssfRow.getCell(11).toString();
            String courseUrl = xssfRow.getCell(12).toString();
            String courseInfo = xssfRow.getCell(13).toString();
            String courseNo = xssfRow.getCell(14).toString();
            String courseTime = xssfRow.getCell(15).toString();
            String likeNum = xssfRow.getCell(16).toString();
            String shareNum = xssfRow.getCell(17).toString();
            String collectNum = xssfRow.getCell(18).toString();
            String lookNum = xssfRow.getCell(19).toString();
            String saleNum = xssfRow.getCell(20).toString();
            String id = xssfRow.getCell(21).toString();
            String createId = xssfRow.getCell(22).toString();
            String createDate = xssfRow.getCell(23).toString();
            String updateId = xssfRow.getCell(24).toString();
            String updateDate = xssfRow.getCell(25).toString();
            String courseLabel = xssfRow.getCell(26).toString();


            //将数据写入实体类
            course.setCourseLabel(courseLabel);
            course.setUpdateDate(DateUtils.transString(updateDate));
            course.setUpdateId(updateId);
            course.setCreateDate(DateUtils.transString(createDate));
            course.setCreateId(createId);
            course.setId(DateUtils.StrisInt(id));
            course.setSaleNum(DateUtils.StrisInt(saleNum));
            course.setLookNum(DateUtils.StrisInt(lookNum));
            course.setCollectNum(DateUtils.StrisInt(collectNum));
            course.setShareNum(DateUtils.StrisInt(shareNum));
            course.setLikeNum(DateUtils.StrisInt(likeNum));
            course.setCourseTime(DateUtils.StrisInt(courseTime));
            course.setCourseNo(DateUtils.StrisInt(courseNo));
            course.setCourseInfo(courseInfo);
            course.setCourseUrl(courseUrl);
            course.setUrl(url);
            course.setCoursePrimary(coursePrimary);
            course.setCourseCstatus(DateUtils.StrisInt(courseCstatus));
            course.setCourseStatus(DateUtils.StrisInt(courseStatus));
            course.setCourseNprice(DateUtils.StrDou(courseNprice));
            course.setCoursePrice(DateUtils.StrDou(coursePrice));
            course.setCourseIntro(courseIntro);
            course.setCourseName(courseName);
            course.setRecommend(DateUtils.StrisInt(recommend));
            course.setClassId(DateUtils.StrisInt(classId));
            course.setUserId(DateUtils.StrisInt(userId));



            System.err.println(course);
            //将实体类插入数据库
            Boolean aBoolean = courseService.addExcel(course);
            return aBoolean != false ? JsonData.buildSuccess(aBoolean) : JsonData.buildError("服务端错误");
        }
        return JsonData.buildError("服务端错误");
    }


    //setCourseStatus  课程列表 上架下架 赵自强 21/12/13 Integer courseStatus
    @RequestMapping("/setCourseStatus")
    public JsonData setCourseStatus(Integer courseStatus,Integer courseId){
        Boolean aBoolean = courseService.setCourseStatus(courseStatus,courseId);
        JsonData jsonData = new JsonData();
        return aBoolean?jsonData.buildSuccess(aBoolean):jsonData.buildError("服务器错误");
    }





}
